package com.cn.wanxi.servlet.pageDataFindAll;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.service.INewsService;
import com.cn.wanxi.service.impl.NewsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@WebServlet("/News")
public class NewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String page=req.getParameter("page");
        String newstype=req.getParameter("newstype");

        NewsModel newsModel=new NewsModel();
        newsModel.setNewstype(newstype);
        newsModel.setPage(page == null || "".equals(page) ? 1:Integer.parseInt(page));



        INewsService newsService=new NewsServiceImpl();

        List<NewsModel> newstypeFind=newsService.newstypeFind(newsModel);
        List<NewsModel> NewsTypeFindAll=newsService.NewsTypeFindAll();
        List<NewsModel> newsNimitFind=newsService.newsNimitFind(newsModel);
        int number=newsService.getTypeCont(newsModel);

        JSONObject jsonObject=new JSONObject();
        Map<String, Object> map = new HashMap<>();
        map.put("newstypeFind", newstypeFind);//根据类型获得所有新闻数据
        map.put("NewsTypeFindAll", NewsTypeFindAll);//获得新闻类型
        map.put("newsNimitFind", newsNimitFind);//获得分页查询数据
        map.put("number", number);//获得总条数
        Set<String> keys = map.keySet();
        for (String key : keys) {
            jsonObject.put(key,map.get(key));//获得总条数
        }
        resp.getWriter().println(jsonObject);
    }
}
